package com.eqdd.yiqidian.ui.gongneng.kehu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.yiqidian.LianxirenInfoActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.HuifangRecordListResultBean;
import com.eqdd.yiqidian.bean.rebuild.LianxirenListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ZhangPicResult;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.ShowRichTextActivity;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.google.gson.Gson;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;

public class HuifangRecordInfoActivity extends SlimBaseMultiEditActivity {
    private LianxirenInfoActivityCustom dataBinding;
    private boolean isUpdate = false;
    private String cusid;
    private String contacts;
    private String clientName;
    private String huifangRecordid;
    private ActionSheetDialog huifangTypeDialog;
    private String revisitRecordid;
    private HuifangRecordListResultBean items;
    private ZhangPicResult<HuifangRecordListResultBean> zhangPicResult;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_lianxiren_info);
        initTopTitleBar(View.VISIBLE, "回访记录");
        isSave = false;
        isRead = false;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 4:
                RichTextResult richTextResult = new RichTextResult();
                richTextResult.setContent(items.getRevisitcontent());
                if (zhangPicResult.getPicturesAddr() != null) {
                    ArrayList<LocalMedia> localMedias = new ArrayList<>();
                    for (int i = 0; i < zhangPicResult.getPicturesAddr().size(); i++) {
                        localMedias.add(PicUtil.localMedia(zhangPicResult.getPicturesAddr().get(i)));
                    }
                    richTextResult.setSelectMedia(localMedias);
                }
                Bundle bundle = new Bundle();
                bundle.putString(Config.TITLE, "回访内容");
                bundle.putParcelable(Config.RICH_TEXT_RESULT, richTextResult);
                startActivity(ShowRichTextActivity.class, bundle);
                break;
        }
    }

    @Override
    protected void onTimeClick(int position) {
        super.onTimeClick(position);
        update();
    }

    @Override
    public void setView() {
        cusid = getIntent().getStringExtra("cusid");
        revisitRecordid = getIntent().getStringExtra("revisitRecordid");
        clientName = getIntent().getStringExtra(Config.CLIENT_NAME);
        OkGo.<ZhangPicResult<HuifangRecordListResultBean>>post(HttpConfig.BASE_URL + HttpConfig.HUIFANG_INFO)
                .params("owner", user.getGuid())
                .params("recordId", revisitRecordid)
                .execute(new DialogCallBack<ZhangPicResult<HuifangRecordListResultBean>>(HuifangRecordInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<ZhangPicResult<HuifangRecordListResultBean>> response) {
                        zhangPicResult = response.body();
                        ToastUtil.showShort(zhangPicResult.getMsg());
                        if (zhangPicResult.getStatus() == 200) {
                            items = zhangPicResult.getItems();
                            datas.add(new SlimEditHintBean("回访标题", items.getRevisitTitle(), 0));
                            datas.add(new SlimEditHintBean("客户名称", items.getContactsName(), 1));
                            datas.add(new SlimEditHintBean("回访类型", items.getRevisitType(), 2));
                            datas.add(new SlimEditHintBean("回访日期", items.getRevisitDate(), 3));
                            datas.add(new SlimEditEnterBean("回访内容", items.getRevisitcontent(), 4, true, ""));
                            datas.add(new SlimEditTimeBean("提醒(再次回访)", items.getRemindTime(), 5).setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE));
                            datas.add(new SlimEditHintBean("创建时间", items.getCreateTime(), 6));
                            slimAdapterEx.updateData(datas);
                        }
                    }
                });
    }

    @Override
    protected void setEditBeans() {

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);

    }

    public void update() {
        OkGo.<ZhangPicResult>post(HttpConfig.BASE_URL + HttpConfig.KEHU_UPDATE_HUIFANG_RECORD)
                .params("owner", user.getGuid())
                .params("revisitRecordid", huifangRecordid)
                .params("data", formatParam())
                .execute(new DialogCallBack<ZhangPicResult>(HuifangRecordInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<ZhangPicResult> response) {
                        ZhangPicResult ZhangPicResult = response.body();
                        ToastUtil.showShort(ZhangPicResult.getMsg());
                        if (ZhangPicResult.getStatus() == 200) {
                            isUpdate = true;
                        }
                    }
                });
    }

    private String formatParam() {
        HashMap<String, Object> map = new HashMap<>();
//        map.put("revisitTitle", datas.get(0).getContent());
//        map.put("cusName", clientName);
//        map.put("contactsid", contacts);
//        map.put("revisitDate", datas.get(3).getContent());
//        map.put("revisitcontent", datas.get(5).getContent());
        map.put("remindTime", datas.get(5).getContent());

        return new Gson().toJson(map);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == Config.INTENT_CONTENT) {
//            String content = data.getStringExtra("content");
//            datas.get(requestCode).setContent(content);
//            slimAdapterEx.notifyItemChanged(requestCode);
//            update();
//        }
//        if (resultCode == Config.SUCCESS) {
//            switch (requestCode) {
//                case Config.CHOOSE_LIANXIREN:
//                    LianxirenListResultBean bean = (LianxirenListResultBean) data.getSerializableExtra("bean");
//                    contacts = bean.getId() + "";
//                    slimAdapterEx.getData().get(2).setContent(bean.getName());
//                    slimAdapterEx.notifyItemChanged(2);
//                    break;
//            }
//        }
    }

    @Override
    public void onBackPressed() {
        if (isUpdate) {
            Intent intent = new Intent();

            items.setRemindTime(slimAdapterEx.getDataItem(5).getContent());
            intent.putExtra("bean", items);
            setResult(Config.SUCCESS, intent);
        }
        super.onBackPressed();

    }
}
